const db = require('../db/index')

// 维修分类
exports.getCategory = (req, res) => {
    const sql = 'SELECT id, category_name FROM repair_category'
    db.query(sql, (err, resltus) => {
        if(err) res.cc(err)
        if(resltus.length < 1) return res.cc('获取维修分类失败',0)
        res.send({
            status: 0,
            message: '获取维修分类成功',
            data: resltus
        })
    })
}

// 获取维修列表
exports.getCategoryList = (req, res) => {
    const sql = 'SELECT * FROM repair_service'
    db.query(sql, (err, resltus) => {
        if(err) res.cc(err)
        if(resltus.length < 1) return res.cc('获取维修列表失败',0)
        res.send({
            status: 0,
            message: '获取维修列表成功',
            data: resltus
        })
    })
}

// 根据分类id 获取维修列表
exports.getCategoryListForId = (req, res) => {
    const sql = 'SELECT * FROM repair_service where category_id=?'
    // console.log(req.params.category_id)
    db.query(sql, req.params.category_id ,(err, resltus) => {
        if(err) res.cc(err)
        if(resltus.length < 1) return res.cc('获取维修列表失败')
        res.send({
            status: 0,
            message: '获取维修列表成功',
            data: resltus
        })
    })
}

// 根据维修种类id获取维修种类详细
exports.getServiceItem = (req, res) => {
    const sql = 'SELECT id, service_name, service_pice, service_img from repair_service WHERE id=?'
    // console.log(req.params.id)
    db.query(sql, req.params.id ,(err, resltus) => {
        if(err) res.cc(err)
        if(resltus.length < 1) return res.cc('获取维修列表失败')
        res.send({
            status: 0,
            message: '获取维修列表成功',
            data: resltus
        })
    })
}

// 获取维修种类--收费明细
exports.getServiceClassify = (req, res) => {
    const sql = 'SELECT id, service_name, service_mony from service_item where service_id=? AND service_classify=?'
    // console.log(req.params.id)
    db.query(sql, [req.params.serviceId, req.params.classify ],(err, resltus) => {
        if(err) res.cc(err)
        if(resltus.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: resltus
        })
    })
}


//获取搜索列表
exports.getSearchList = (req, res) => {
    const sql = 'SELECT * FROM repair_service WHERE service_name LIKE ?'
    // console.log(req.params.id)
    db.query(sql, `%${req.query.search}%`,(err, resltus) => {
        if(err) res.cc(err)
        if(resltus.length < 1) return res.cc('没有相关搜索')
        res.send({
            status: 0,
            message: '获取成功',
            data: resltus
        })
    })
}